package top.kaoshanji.p1ch02d;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * 代码清单 p1ch02d-08：MethodHandles#permuteArguments的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/3 18:47
 */
public class MethodHandlesExample08 {

    private static final Logger logger = LogManager.getLogger(MethodHandlesExample08.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02d-08：MethodHandles#permuteArguments的使用示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodType type = MethodType.methodType(int.class, int.class, int.class);
        MethodHandle mhCompare = lookup.findStatic(Integer.class, "compare", type);
        int value = (int)mhCompare.invoke(3, 4);
        System.out.println(value);

        MethodHandle mhNew = MethodHandles.permuteArguments(mhCompare, type, 1, 0);
        value = (int)mhNew.invoke(3, 4);
        System.out.println(value);

        MethodHandle mhDuplicateArgs = MethodHandles.permuteArguments(mhCompare, type, 1, 1);
        value = (int)mhDuplicateArgs.invoke(3, 4);
        System.out.println("PERMUTE " + value);

    }

}
